package com.shop.operator.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.dao.SpecificationDao;
import com.shop.domain.PageResult;
import com.shop.domain.SpecificationCustom;
import com.shop.mapper.SpecificationMapper;
import com.shop.mapper.SpecificationOptionMapper;
import com.shop.model.Specification;
import com.shop.model.SpecificationExample;
import com.shop.model.SpecificationExample.Criteria;
import com.shop.model.SpecificationOption;
import com.shop.model.SpecificationOptionExample;
import com.shop.operator.service.SpecificationService;

@Service("specificationSer")
public class SpecificationServiceBean implements SpecificationService {

	@Resource
	private SpecificationMapper specificationMapper;
	
	@Resource
	private SpecificationDao specificationDao;
	
	@Resource
	private SpecificationOptionMapper specificationOptionMapper;
	
	@Override
	public List<Specification> findAll() throws Exception {
		List<Specification> specifications = this.specificationMapper.selectByExample(null);
		return specifications;
	}

	@Override
	public PageResult findPage(String specName,int pageNum,int pageSize) throws Exception {
		PageHelper.startPage(pageNum, pageSize);//分页
		
		SpecificationExample specificationExample = new SpecificationExample();
		Criteria criteria = specificationExample.createCriteria();
		if(specName!=null && specName.length()>0){
			criteria.andSpecNameLike("%"+specName+"%");
		}
		
		Page<Specification> page = (Page<Specification>) specificationMapper.selectByExample(specificationExample);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);//分页
		Page<Specification> page = (Page<Specification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(SpecificationCustom specificationCustom)throws Exception {
		//获取规格实体
		Specification specification = specificationCustom.getSpecification();
		//添加规格
		this.specificationDao.insert(specification);
		
		List<SpecificationOption> specificationOptions = specificationCustom.getSpecificationOptionList();
		for(SpecificationOption specificationOption:specificationOptions){
			specificationOption.setSpecId(specification.getId());//设置规格id
			this.specificationOptionMapper.insert(specificationOption);
		}
	}

	@Override
	public List<Specification> findName(String specName) throws Exception {
		SpecificationExample example = new SpecificationExample();
		Criteria criteria = example.createCriteria();
		criteria.andSpecNameEqualTo(specName);
		List<Specification> specifications = this.specificationMapper.selectByExample(example);
		return specifications;
	}

	@Override
	public SpecificationCustom findOne(long id) throws Exception {
		SpecificationCustom specificationCustom = new SpecificationCustom();
		//根据id查找对象放到规格类的扩展类中
		specificationCustom.setSpecification(this.specificationMapper.selectByPrimaryKey(id));
		
		//根据规格id查询规格选项列表
		SpecificationOptionExample specificationOptionExample = new SpecificationOptionExample();
		com.shop.model.SpecificationOptionExample.Criteria criteria = specificationOptionExample.createCriteria();
		criteria.andSpecIdEqualTo(id);
		List<SpecificationOption> specificationOptions = this.specificationOptionMapper.selectByExample(specificationOptionExample);
		
		specificationCustom.setSpecificationOptionList(specificationOptions);
		return specificationCustom;
	}

	@Override
	public void update(SpecificationCustom specificationCustom) throws Exception {
		//获取规格实体
		Specification specification = specificationCustom.getSpecification();
		specificationMapper.updateByPrimaryKey(specification);
		//删除原来规格中对应的规格选项
		SpecificationOptionExample specificationOptionExample = new SpecificationOptionExample();
		com.shop.model.SpecificationOptionExample.Criteria criteria = specificationOptionExample.createCriteria();
		criteria.andSpecIdEqualTo(specification.getId());
		specificationOptionMapper.deleteByExample(specificationOptionExample);
		
		//插入修改后的规格数据
		List<SpecificationOption> specificationOptions = specificationCustom.getSpecificationOptionList();
		for(SpecificationOption specificationOption:specificationOptions){
			specificationOption.setSpecId(specification.getId());
			specificationOptionMapper.insert(specificationOption);
		}
		
		
	}

	@Override
	public void delete(long[] ids) throws Exception {
		for(long id:ids){
			//删除规格表中的数据
			this.specificationMapper.deleteByPrimaryKey(id);
			//删除规格选项表中的数据
			SpecificationOptionExample example = new SpecificationOptionExample();
			com.shop.model.SpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			this.specificationOptionMapper.deleteByExample(example);
		}
		
	}
	@Override
	public List<Map> findSpecList() {
		return specificationMapper.findSpecList();
	}

}
